﻿Imports Microsoft.VisualBasic
Imports System.Web.Services.WebService
Imports EShop.VBClass


''' <summary>
''' 公共模块集合
''' </summary>
''' <remarks></remarks>
Public Module PublicFunctions



#Region "公共属性"
    Dim objSQLDB As New SQLDBClass

    ''' <summary>
    ''' 数据库链接是否已打开
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property SQLIsOpen As Boolean
        Get
            If SQLConn Is Nothing Then
                Return False
            Else
                If SQLConn.Status = Data.ConnectionState.Open Then
                    Return True
                Else
                    Return False
                End If
            End If
        End Get

    End Property

    ''' <summary>
    ''' SQL数据连接对象
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property SQLConn As SQLDBClass
        Get
            Return objSQLDB
        End Get
        Set(value As SQLDBClass)
            objSQLDB = value
        End Set
    End Property


    Dim dtBase As Data.DataTable
    ''' <summary>
    ''' 基本信息数据
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property BaseInfo As Data.DataTable
        Get
            Return dtBase
        End Get
        Set(value As Data.DataTable)
            dtBase = value
        End Set
    End Property

    ''' <summary>
    ''' 按索引号取基本信息内容
    ''' </summary>
    ''' <param name="index">索引号</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property BaseInfoItem(index As Integer) As String
        Get
            If BaseInfo Is Nothing Then Return ""
            Return BaseInfo.Rows(0)(index).ToString
        End Get
    End Property

    ''' <summary>
    ''' 按字段名取基本信息内容
    ''' </summary>
    ''' <param name="fieldName">名称</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property BaseInfoItem(fieldName As String) As String
        Get
            If fieldName Is Nothing Then Return ""
            If fieldName.Trim = "" Then Return ""
            If Not fieldName.StartsWith("bas_") Then
                fieldName = "bas_" & fieldName
            End If
            If BaseInfo Is Nothing Then Return ""
            Try
                Return BaseInfo.Rows(0)(fieldName).ToString
            Catch ex As Exception
                Return ""
            End Try

        End Get
    End Property

    Dim senUid As String = ""
    ''' <summary>
    ''' 当前登陆的管理员ID
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property LoginUid As String
        Get
            Return senUid
        End Get
        Set(value As String)
            senUid = value
        End Set
    End Property


    Dim filConfigs() As String
    ''' <summary>
    ''' 上传文件夹配制列表
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property fileConfigs() As String()
        Get
            Return filConfigs
        End Get
        Set(value As String())
            filConfigs = value
        End Set
    End Property

    ''' <summary>
    ''' 按值获取上传文件夹配制项描述
    ''' </summary>
    ''' <param name="cName">文件夹名</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property fileConfigItem(cName As String) As String
        Get
            If fileConfigs Is Nothing Then
                Return cName
            Else
                Dim cn As String = cName.Trim & ":"
                For Each fcng As String In fileConfigs
                    If fcng.Contains(cn) Then
                        Return fcng.Trim.Replace(cn, "")
                    End If
                Next
                Return cName
            End If
        End Get
    End Property

#End Region

#Region "公共函数"

    ''' <summary>
    ''' 检测登录是否过期
    ''' </summary>
    ''' <param name="webForm">检测的窗体页面(一般用 Me)</param>
    ''' <param name="oType">检测类型 0:后台登陆 1:前台会员登陆</param>
    ''' <remarks></remarks>
    Sub checkSessionTimeout(Optional webForm As UI.Page = Nothing, Optional goURL As String = "~/adminsys/loginout.aspx", Optional oType As Integer = 0)
        If webForm Is Nothing Then webForm.Response.Redirect(goURL, True)
        Dim sesobj As Object = Nothing
        If oType = 0 Then
            sesobj = webForm.Session("admin_UID")
        Else
            sesobj = webForm.Session("user_UID")
        End If
        If sesobj Is Nothing Then
            webForm.Response.Redirect(goURL, True)
        Else
            Dim sesn As String = sesobj.ToString
            If sesn = "" Then
                webForm.Response.Redirect(goURL, True)
            End If
        End If

    End Sub


    ''' <summary>
    ''' 检测是否已打开数据库连接，否则自动打开连接
    ''' </summary>
    ''' <param name="webForm">检测的窗体页面(一般用 Me)</param>
    ''' <remarks></remarks>
    Sub checkData(Optional webForm As Web.UI.Page = Nothing)
        If Not SQLIsOpen Then
            Dim sqlConfig As String = "/adminsys/sqlconfig.dat"
            Dim fPath As String = webForm.Server.MapPath(sqlConfig)
            Dim configs() As String
            If IO.File.Exists(fPath) Then
                Dim strConfigs As String = IO.File.ReadAllText(fPath)

                If strConfigs.Trim <> "" Then
                    strConfigs = DecodeAccString(strConfigs, "zyukeys")
                    configs = strConfigs.Split(vbCrLf)
                    If configs.Length < 3 Then
                        ExecuteJSalert(webForm, "注意：数据库配制信息错误，为保证数据正常访问，请重新配制！")
                        Exit Sub
                    End If
                Else
                    ExecuteJSalert(webForm, "注意：数据库配制信息为空，为保证数据正常访问，请重新配制！")
                    Exit Sub
                End If
            Else
                ExecuteJSalert(webForm, "错误：配制文件不存在，为保证数据正常访问，请配制！")
                Exit Sub
            End If

            Dim dbServer As String = dValue(configs(0), "") ' "14.17.106.229" ' '服务器地址
            Dim dBase As String = dValue(configs(1), "") ' "SOARINGDB"
            Dim uID As String = dValue(configs(2), "") '"SOARUID"
            Dim PWD As String = dValue(configs(3), "") '"SOAR_Master_2349"
            Dim isLocal As Boolean = My.Request.IsLocal
            'If isLocal Then dbServer = "EVENPC" '本地测试地址

            If Not openDatabase(dbServer, dBase, uID, PWD) Then
                ExecuteJSalert(webForm, "数据库链接出错！\n可能网络问题或数据库配制问题!" & _
                               "\n如需要配制数据库，请进入此页面  http://" & "webdress/webprogram/sqlconfig.aspx" & _
                               "\n注：webdress 一般为您的网站域名。")
            Else
                BaseInfo = GetDataTable("baseinfos")
            End If
        Else
            'MsgBox("连接已打开")
            Dim dbr As DBReaders
        End If
    End Sub

    ''' <summary>
    ''' 设置文件上传配制
    ''' </summary>
    ''' <param name="configPath">配制文件</param>
    ''' <remarks></remarks>
    Sub setFileconfigs(configPath As String)
        'Dim filConfig As String = configPath ' Server.MapPath("/webprogram/config.ini")
        If My.Computer.FileSystem.FileExists(configPath) Then
            Dim fConfig As String = My.Computer.FileSystem.ReadAllText(configPath, Encoding.Default)
            fileConfigs = fConfig.Split(vbCrLf)
        Else
            fileConfigs = Nothing
        End If
    End Sub

    ''' <summary>
    ''' 取角色名称
    ''' </summary>
    ''' <param name="roleInd">角色索引</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function RoleName(roleInd As Integer) As String
        Dim rolName As String = "超级管理员|系统管理员|网站管理员|信息管理员|产品管理员"
        '1:超级管理员  2:系统管理员 3:网站管理员  4:信息管理员  5:产品管理员
        Dim rolNames = rolName.Split("|")
        If roleInd <= UBound(rolNames) Then
            Return rolNames(roleInd - 1)
        Else
            Return "错误角色:" & roleInd
        End If
    End Function

    ''' <summary>
    ''' 获取是或否
    ''' </summary>
    ''' <param name="v">传值(boolean)</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getYesNo(v As Boolean) As String
        If v = Nothing Then Return "否"
        If v Then Return "<span class=red>是</span>" Else Return "否"
    End Function

    ''' <summary>
    ''' 获取布尔 是转1 否转0
    ''' </summary>
    ''' <param name="v">传值(boolean)</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getYesNoN(v As Boolean) As Integer
        If v = Nothing Then Return "0"
        If v Then Return "1" Else Return "0"
    End Function

    ''' <summary>
    ''' 获取是或否
    ''' </summary>
    ''' <param name="v">传值(Number)</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getYesNo(v As Integer) As String
        If v = Nothing Then Return "否"
        If v = 0 Then Return "否" Else Return "<span class=red>是</span>"
    End Function

    ''' <summary>
    ''' 设置数字转布尔
    ''' </summary>
    ''' <param name="v">传值(Number)</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function setNumtoBoolean(v As Integer) As Boolean
        If v = Nothing Then Return False
        If v = 0 Then Return False Else Return True
    End Function

    ''' <summary>
    ''' 绑定基本信息列表信息
    ''' </summary>
    ''' <param name="baseItemName">基本信息名称</param>
    ''' <param name="dropList">绑定下拉控件</param>
    ''' <remarks></remarks>
    Sub bindBsitemList(baseItemName As String, dropList As DropDownList)
        Dim bsItem As String = BaseInfoItem(baseItemName)
        Dim bsItems() As String = bsItem.Split(vbCrLf)
        dropList.Items.Clear()
        For UI As Integer = 0 To UBound(bsItems)
            dropList.Items.Add(bsItems(UI).Trim)
        Next
    End Sub

#Region "后台应用"

    ''' <summary>
    ''' 获取产品分类下拉列表(后台用)
    ''' </summary>
    ''' <param name="topId">顶级分类ID</param>
    ''' <param name="dropList">绑定的下拉控件</param>
    ''' <param name="selID">选中的ID值</param>
    ''' <param name="showAll">是否显示全部选项</param>
    ''' <remarks></remarks>
    Sub getClassDrorList(topId As Integer, dropList As DropDownList, Optional selID As Integer = 0, Optional showAll As Boolean = False, Optional isTop As Boolean = True)
        Dim t_id, tBid As String
        Dim strCond As String = ""
        If isTop Then
            dropList.Items.Clear()
            'If clsType = 0 Then
            '    strCond = "id=" & topId
            'Else
            tBid = "T" & FillNumber(topId, 3)
            t_id = getFiledvalue("id", "classes", "cls_topid='" & tBid & "'")
            If t_id <> "" Then
                strCond = "cls_parentid=" & t_id '& " and cls_Type=" & clsType
            Else
                strCond = "cls_parentid=" & topId '& " and cls_Type=" & clsType
            End If

            'End If
        Else
            'If clsType = 0 Then
            '    strCond = "id=" & topId
            'Else
            strCond = "cls_parentid=" & topId '& " and cls_Type=" & clsType
            'End If
        End If


        Dim subNum As Integer = 0
        Dim dtClass As Data.DataTable = GetDataTable("Classes", "*", strCond, "cls_pSort desc")
        Dim dtClass2 As Data.DataTable = Nothing
        Dim cls_Name As String, cls_id, cls_Degree As Integer, cls_serName As String, strSeld As String = ""
        Dim lstItem As ListItem
        Dim strDot As String = ""

        If dtClass IsNot Nothing Then
            If showAll Then
                lstItem = New ListItem("== 全部 ==", "")
                dropList.Items.Add(lstItem)
            End If

            Dim rCount As Integer = dtClass.Rows.Count
            For r As Integer = 0 To rCount - 1
                cls_Name = dtClass.Rows(r)("cls_Name").ToString
                cls_id = dtClass.Rows(r)("id")
                cls_Degree = dtClass.Rows(r)("cls_Degree")
                cls_serName = dValue(dtClass.Rows(r)("cls_serName"), "未指定:")
                If isTop Then topDegree = cls_Degree - 1
                strDot = ""
                For i As Integer = 1 To cls_Degree - topDegree
                    strDot &= " … "
                Next
                lstItem = New ListItem(strDot & cls_Name, cls_id)
                lstItem.Attributes.Add("serName", cls_serName)
                If selID = cls_id Then
                    lstItem.Selected = True
                Else
                    lstItem.Selected = False
                End If
                dropList.Items.Add(lstItem)
                'strClassList &= "<li class=""sort" & (cls_Degree - topDegree) & strSeld & """><a href=""" & href & "?pdid=" & cls_id & """>" & cls_Name & "</a></li>" & vbCrLf
                Dim sParameter() As Data.SqlClient.SqlParameter = { _
                      New Data.SqlClient.SqlParameter("@parentID", cls_id) _
                     , New Data.SqlClient.SqlParameter("@tableName", "classes") _
                     , New Data.SqlClient.SqlParameter("@parentidField", "cls_parentid") _
                  }
                dtClass2 = GetDataTableP("getSubclassNum ", sParameter)
                subNum = dtClass2.Rows(0)("subnum")
                If subNum > 0 Then
                    getClassDrorList(cls_id, dropList, selID, False, False)
                End If
            Next
        End If
    End Sub

    ''' <summary>
    ''' 获取后台菜单
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getMenulist(Optional roleid As String = "0") As String
        Dim strMeu1 As New StringBuilder, strMeu2 As String = ""
        Dim dtMeu1 As Data.DataTable = GetDataTable("LMenus", "*", "meu_parentid=0 and meu_isShow=1", "meu_psort desc")
        Dim dtMeu2 As Data.DataTable = Nothing
        Dim rCount1 As Integer = 0, rCount2 As Integer = 0
        Dim meu_Name, meu_href, meu_href2 As String
        Dim meuID As Integer, meuID2 As Integer

        If dtMeu1 IsNot Nothing Then
            rCount1 = dtMeu1.Rows.Count
            For r As Integer = 0 To rCount1 - 1
                meu_Name = dtMeu1.Rows(r)("meu_Name").ToString
                'meu_href = dtMeu1.Rows(r)("meu_href").ToString
                meuID = dtMeu1.Rows(r)("id")
                strMeu1.Append("<div class=""meuTable"" >" & vbCrLf)
                strMeu1.Append("    <div class=""meu_show"" onclick=""SetVisible('Menu" & meuID & "',this)"">" & meu_Name & "</div>" & vbCrLf)
                strMeu1.Append("    <div id=""Menu" & meuID & """ class=""meuListHide"" >" & vbCrLf)
                If meuID = 1 Then
                    If LoginUid = "supadmin0" Then strMeu1.AppendLine("<a href=""lmenus.aspx"" target=""frmMain"" class=""adminnav1"">菜单配制</a><br />")
                End If
                dtMeu2 = GetDataTable("LMenus", "*", "meu_parentid=" & meuID & " and meu_isShow=1", "meu_psort desc")
                strMeu2 = ""
                If dtMeu2 IsNot Nothing Then
                    rCount2 = dtMeu2.Rows.Count
                    For r2 As Integer = 0 To rCount2 - 1
                        meuID2 = dtMeu2.Rows(r2)("id").ToString
                        meu_Name = dtMeu2.Rows(r2)("meu_Name").ToString
                        meu_href = dtMeu2.Rows(r2)("meu_href").ToString
                        If meu_href = "" Then
                            meu_href = "javascript:alert('暂时未绑定页面！')"
                        End If
                        Select Case meuID2
                            Case 13, 21, 22
                                If LoginUid = "wen" Then strMeu2 &= "    <a href=""" & meu_href & """ target=""frmMain"" class=""adminnav1"">" & meu_Name & "</a><br />" & vbCrLf
                            Case Else
                                strMeu2 &= "    <a href=""" & meu_href & """ target=""frmMain"" class=""adminnav1"">" & meu_Name & "</a><br />" & vbCrLf
                        End Select

                    Next
                Else
                    strMeu2 = "<span class=dgray>暂无菜单</span>"
                End If

                strMeu1.Append(strMeu2 & "    </div>" & vbCrLf)
                strMeu1.Append("    <div ><img src=""images/pix1.gif"" width=""1"" height=""16"" /></div>" & vbCrLf)
                strMeu1.Append("</div>" & vbCrLf)
            Next
        End If

        Return strMeu1.ToString
    End Function

#Region "后台订单管理_会员信息"


    ''' <summary>
    ''' 绑定客户到下拉列表框
    ''' </summary>
    ''' <param name="dropList">绑定的控件</param>
    ''' <remarks></remarks>
    Sub bindUsers(dropList As DropDownList)
        Dim SQLU As String = "select distinct odr_uid,(select use_UID + '('+ use_xname + ')' from users where id=odr_uid) as uName from orders "
        Dim dtU As Data.DataTable = GetDataTableA(SQLU)
        dropList.Items.Clear()
        If dtU IsNot Nothing Then
            Dim nRow As Data.DataRow = dtU.NewRow
            nRow.Item(0) = -9999
            nRow.Item(1) = "==全部=="
            dtU.Rows.InsertAt(nRow, 0)
            dropList.DataTextField = "uName"
            dropList.DataValueField = "odr_uid"
            dropList.DataSource = dtU
            dropList.DataBind()
        Else
            Dim itemAll As New ListItem
            itemAll.Text = "==全部=="
            itemAll.Value = "-9999"
            dropList.Items.Add(itemAll)
        End If
    End Sub



#End Region

#End Region

#Region "前台应用"


    Dim strClassList As String = ""
    Dim topDegree As Integer = 0
    'Dim dtClassList As New Data.DataTable

    ''' <summary>
    ''' 获取产品分类(前台页面用)
    ''' </summary>
    ''' <param name="topId">顶级菜单ID</param>
    ''' <param name="href">链接地址</param>
    ''' <param name="selID">选中的ID</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getClassList(topId As Integer, Optional href As String = "#,#", Optional selID As Integer = 0, Optional isTop As Boolean = True) As String
        If isTop Then strClassList = "<li class=""sort-t""> <a href='prodnav.aspx'>产品导航</a></li>"
        Dim strCond As String = " cls_parentid=" & topId '& " and cls_Type=1"
        Dim subNum As Integer = 0
        Dim dtClass As Data.DataTable = GetDataTable("Classes", "*", strCond, "cls_pSort desc")
        Dim dtClass2 As Data.DataTable = Nothing ' GetDataTable("select count(id) from classes where cls_parentid=id")
        Dim cls_Name As String, cls_id, cls_Degree As Integer, strSeld As String = ""
        Dim href1, href2, hrefto As String, hrefs() As String
        hrefs = href.Split(",")
        href1 = hrefs(0)
        If UBound(hrefs) > 0 Then
            href2 = hrefs(1)
        Else
            href2 = href1
        End If
        If dtClass IsNot Nothing Then
            Dim rCount As Integer = dtClass.Rows.Count
            For r As Integer = 0 To rCount - 1
                cls_Name = dtClass.Rows(r)("cls_Name").ToString
                cls_id = dtClass.Rows(r)("id")
                cls_Degree = dtClass.Rows(r)("cls_Degree")
                If isTop Then topDegree = cls_Degree - 1
                If selID = cls_id Then
                    strSeld = " sortselect "
                Else
                    strSeld = ""
                End If
                Dim sParameter() As Data.SqlClient.SqlParameter = { _
                      New Data.SqlClient.SqlParameter("@parentID", cls_id) _
                     , New Data.SqlClient.SqlParameter("@tableName", "classes") _
                     , New Data.SqlClient.SqlParameter("@parentidField", "cls_parentid") _
                  }
                dtClass2 = GetDataTableP("getSubclassNum ", sParameter)
                subNum = dtClass2.Rows(0)("subnum")
                If subNum > 0 Then hrefto = href1 Else hrefto = href2
                strClassList &= "<li class=""sort" & (cls_Degree - topDegree) & strSeld & """><a href=""" & hrefto & "?plid=" & cls_id & """>" & cls_Name & "</a></li>" & vbCrLf
                If subNum > 0 Then
                    strClassList = getClassList(cls_id, href, selID, False)
                Else

                End If
            Next
        End If
        Return strClassList
    End Function

    ''' <summary>
    ''' 获取新闻分类
    ''' </summary>
    ''' <param name="topId">顶级菜单ID</param>
    ''' <param name="href">链接地址</param>
    ''' <param name="selID">选中的ID</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getNewsclsList(topId As Integer, Optional href As String = "#", Optional selID As Integer = 0, Optional isTop As Boolean = True) As String
        Dim strCond As String = ""
        Dim tBid As String = ""
        Dim t_id As String = ""
        If isTop Then
            strClassList = ""
            tBid = "T" & FillNumber(topId, 3)
            t_id = getFiledvalue("id", "classes", "cls_topid='" & tBid & "'")
            strCond = " cls_parentid=" & t_id '& " and cls_Type=1"
        Else
            strCond = " cls_parentid=" & topId '& " and cls_Type=1"
        End If



        Dim subNum As Integer = 0, degreeNum As Integer = 0
        Dim dtClass As Data.DataTable = GetDataTable("Classes", "*", strCond, "cls_pSort desc")
        Dim dtClass2 As Data.DataTable = Nothing ' GetDataTable("select count(id) from classes where cls_parentid=id")
        Dim cls_Name As String, cls_id, cls_Degree As Integer, strSeld As String = "", strDegree As String = ""
        If dtClass IsNot Nothing Then
            Dim rCount As Integer = dtClass.Rows.Count
            For r As Integer = 0 To rCount - 1
                cls_Name = dtClass.Rows(r)("cls_Name").ToString
                cls_id = dtClass.Rows(r)("id")
                cls_Degree = dtClass.Rows(r)("cls_Degree")
                If isTop Then topDegree = cls_Degree - 1
                If selID = cls_id Then
                    strSeld = " select "
                Else
                    strSeld = ""
                End If
                degreeNum = IIf(cls_Degree - 2 >= 0, cls_Degree - 2, 0)
                strDegree = StringNum(" &nbsp; ", degreeNum * 2)
                strClassList &= "<li class=""" & strSeld & """><a href=""" & href & "?nlid=" & cls_id & """>" & strDegree & cls_Name & "</a></li>" & vbCrLf
                Dim sParameter() As Data.SqlClient.SqlParameter = { _
                      New Data.SqlClient.SqlParameter("@parentID", cls_id) _
                     , New Data.SqlClient.SqlParameter("@tableName", "classes") _
                     , New Data.SqlClient.SqlParameter("@parentidField", "cls_parentid") _
                  }
                dtClass2 = GetDataTableP("getSubclassNum ", sParameter)
                subNum = dtClass2.Rows(0)("subnum")
                If subNum > 0 Then
                    strClassList = getNewsclsList(cls_id, href, selID, False)
                End If
            Next
        End If
        Return strClassList
    End Function

    ''' <summary>
    ''' 加载新闻标题列表
    ''' </summary>
    ''' <param name="clsID">分类顶ID</param>
    ''' <param name="serKey">搜索关键词</param>
    ''' <param name="showTime">显示更新时间</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getNewsList(Optional clsID As String = "", Optional serKey As String = "", Optional showTime As Boolean = False, Optional lCount As Integer = 0) As String
        clsID = dValue(clsID, "2")
        Dim subIds As String = getSubIDs(clsID, True, True)
        Dim cond As String = " (cnt_clsID in(" & subIds & ") and cnt_stop=0) "
        If serKey <> "" Then
            cond &= " and (cnt_Title like '%" & serKey & "%'  or cnt_Intro like '%" & serKey & "%' or cnt_Content like '%" & serKey & "%' ) "
        End If
        Dim strCount As String = ""
        If lCount <> 0 Then
            strCount = " top(" & lCount & ")"
        End If
        Dim field As String = strCount & "Id,cnt_Title,cnt_clsID,cnt_Picx,cnt_UpdTime,cnt_cType,cnt_linkTo"
        Dim strNewlst As New StringBuilder
        Dim dtNew As Data.DataTable = GetDataTable("Contentexts", field, cond, " cnt_pSort desc,cnt_UpdTime desc")
        If dtNew IsNot Nothing Then
            Dim rCount As Integer = dtNew.Rows.Count
            Dim nId, cnt_Title, cnt_clsID, cnt_Picx, cnt_UpdTime, cnt_cType, cnt_linkTo As String
            Dim clsName, strImglog, href, strRecLine As String
            For r As Integer = 0 To rCount - 1
                nId = dtNew.Rows(r)("id")
                cnt_Title = dtNew.Rows(r)("cnt_Title")
                cnt_clsID = dtNew.Rows(r)("cnt_clsID")
                cnt_Picx = dValue(dtNew.Rows(r)("cnt_Picx"), "")
                cnt_UpdTime = dtNew.Rows(r)("cnt_UpdTime")
                cnt_cType = dtNew.Rows(r)("cnt_cType")
                cnt_linkTo = dValue(dtNew.Rows(r)("cnt_linkTo"), "")
                clsName = getFiledvalue("cls_name", "classes", "id=" & cnt_clsID)
                clsName = "<span class=gray> [" & clsName & "] </span>"
                If cnt_Picx <> "" Then
                    strImglog = " <span class=org>[图]</span> "
                Else
                    strImglog = ""
                End If
                If cnt_cType = "0" Then
                    href = "newsdetail.aspx?nid=" & nId
                Else
                    href = cnt_linkTo
                End If
                If showTime Then
                    strRecLine = "<tr><td><img src=""images/dot.gif"" />{0} {1} <a href=""{2}"" target=""_blank"">{3}</a></td><td class='gray txtRight'>{4}</td></tr>"
                    strNewlst.AppendFormat(strRecLine, clsName, strImglog, href, cnt_Title, cnt_UpdTime)
                Else
                    strRecLine = "<li>{0} <a href=""{1}"" target=""_blank""> {2} </a></li>"
                    strNewlst.AppendFormat(strRecLine, strImglog, href, cnt_Title)
                End If
            Next
        End If
        Return strNewlst.ToString
    End Function



    ''' <summary>
    ''' 获取内容
    ''' </summary>
    ''' <param name="cid">当前内容ID</param>
    ''' <param name="mTitle">模块标题</param>
    '''  <param name="webForm">网页窗体(一般用Me)</param>
    ''' <param name="ctlTitle">标题显示控件(一般是带runet=Server的HTML元素)</param>
    ''' <param name="ctlContent">内容显示控件(一般是带runet=Server的HTML元素)</param>
    ''' <remarks></remarks>
    Sub getCntent(cid As Integer, Optional mTitle As String = "", Optional webForm As Web.UI.Page = Nothing, Optional ctlTitle As UI.HtmlControls.HtmlGenericControl = Nothing, Optional ctlContent As UI.HtmlControls.HtmlGenericControl = Nothing)
        Dim dtCnt As Data.DataTable = GetDataTable("Contentexts", "*", "id=" & cid, "")
        Dim strCnt, strTitle, titles As String
        Dim keyWord, Description As String
        Dim cnt_cType As Integer = 0
        Dim cnt_Linkto As String = ""
        If dtCnt IsNot Nothing Then
            strCnt = dtCnt.Rows(0)("cnt_Content").ToString
            strTitle = dtCnt.Rows(0)("cnt_title").ToString
            keyWord = dtCnt.Rows(0)("cnt_keyWord").ToString
            cnt_cType = dtCnt.Rows(0)("cnt_cType")
            cnt_Linkto = dtCnt.Rows(0)("cnt_Linkto").ToString
            'If cnt_cType = 0 Then
            Description = dtCnt.Rows(0)("cnt_KeyDescription").ToString
            If strCnt = "" Then strCnt = "还未添加 " & strTitle & " 的信息，请在后台添加"
            'Else
            '   Description = ""
            'End If
        Else
            strTitle = "-"
            strCnt = "无相关数据，请在后台添加"
            keyWord = ""
            Description = ""
            cnt_cType = 0
            cnt_Linkto = ""
        End If
        titles = "<span>" & mTitle & "</span> " & strTitle
        webForm.Title = mTitle & "-" & strTitle
        Dim metaKeyWord As HtmlMeta = webForm.Header.FindControl("keywords")
        Dim metaDescription As HtmlMeta = webForm.Header.FindControl("description")

        If keyWord <> "" And metaKeyWord IsNot Nothing Then
            metaKeyWord.Attributes("content") = keyWord
        End If

        If Description <> "" And metaKeyWord IsNot Nothing Then
            metaDescription.Attributes("content") = Description
        End If
        If ctlTitle IsNot Nothing Then
            ctlTitle.InnerHtml = titles
        End If
        If cnt_cType = 0 Then   '编辑内容
            If ctlContent IsNot Nothing Then
                ctlContent.InnerHtml = strCnt
            End If
        ElseIf cnt_cType = 1 Then   '地址连接
            'ExecuteJS(webForm, "window.open('" & cnt_Linkto & "','newwind');window.history.go(-1);")
            'webForm.Form.Page.Response.Redirect(cnt_Linkto)
        ElseIf cnt_cType = 2 Then   '控件链接
            Dim fLink As String = "~/" & cnt_Linkto & ".ascx"
            If IO.File.Exists(webForm.Server.MapPath(fLink)) Then
                ctlContent.Controls.Add(webForm.LoadControl(fLink))
            Else
                ctlContent.InnerHtml = "控件地址不存在"
            End If
        Else
            If ctlContent IsNot Nothing Then
                ctlContent.InnerHtml = strCnt
            End If
        End If

    End Sub


    Dim allIds As String = ""
    ''' <summary>
    ''' 获取子分类ID集合
    ''' </summary>
    ''' <param name="topID">顶分类</param>
    ''' <param name="isTop">是否顶(默认True即可)</param>
    ''' <param name="ContainSelf">是否包含自身</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getSubIDs(topID As Integer, Optional isTop As Boolean = True, Optional ContainSelf As Boolean = False, Optional allSub As Boolean = True) As String
        Dim pid As String
        Dim dtIds As Data.DataTable = GetDataTable("classes", "id", "cls_parentid=" & topID, "")
        Dim dtsIds As Data.DataTable = Nothing
        Dim rCount, r As Integer
        If isTop Then
            If ContainSelf Then
                allIds = topID
            Else
                allIds = ""
            End If
        End If

        If dtIds IsNot Nothing Then
            rCount = dtIds.Rows.Count
            For r = 0 To rCount - 1
                pid = dtIds.Rows(r)("id")
                If allIds = "" Then allIds = pid Else allIds &= "," & pid
                If allSub Then allIds = getSubIDs(pid, False, False, allSub)
            Next
        End If
        Return allIds
    End Function

    ''' <summary>
    ''' 获取内容菜单(公司简介等)
    ''' </summary>
    ''' <param name="clsiID">所属分类ID</param>
    '''  <param name="href">转向超链接</param>
    '''  <param name="selID">默认选中ID</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getContentMenu(clsiID As Integer, Optional href As String = "#", Optional selID As Integer = 0) As String
        Dim strMenus As New StringBuilder
        Dim sqlM As String = ""
        Dim dtMeu As Data.DataTable = GetDataTable("Contentexts", "cnt_title,id,cnt_clsID,cnt_cType,cnt_linkto", "cnt_clsID=" & clsiID, "cnt_pSort desc")
        If dtMeu IsNot Nothing Then
            Dim rCount As Integer = dtMeu.Rows.Count
            Dim goHref, cnt_titme, strSeld, target As String
            Dim curID, cnt_clsID, cnt_ctype As Integer

            For r As Integer = 0 To rCount - 1
                cnt_titme = dtMeu.Rows(r)("cnt_title")
                curID = dtMeu.Rows(r)("id")
                cnt_clsID = dtMeu.Rows(r)("cnt_clsID")
                cnt_ctype = dtMeu.Rows(r)("cnt_ctype")
                goHref = href & "?cid=" & curID
                If selID.ToString.Trim = curID.ToString.Trim Then
                    strSeld = "select"
                Else
                    strSeld = ""
                End If
                If cnt_ctype = 1 Then
                    target = " target='_blank' "
                    goHref = dtMeu.Rows(r)("cnt_linkto")
                Else
                    target = ""
                End If
                strMenus.Append("<li class=""" & strSeld & """><a href=""" & goHref & """ " & target & ">" & cnt_titme & "</a></li>")
            Next
        End If
        Return strMenus.ToString
    End Function


    ''' <summary>
    ''' 获取默认ID
    ''' </summary>
    ''' <param name="clsID">主分类ID</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getDefaultID(clsID As Integer) As String
        Dim strDfid As String = ""
        strDfid = getFiledvalue("id", "Contentexts", "cnt_clsId=" & clsID & " order by cnt_psort desc")
        If strDfid = "" Then strDfid = "0"
        Return strDfid
    End Function


    ''' <summary>
    ''' 获取产品缩略列表
    ''' </summary>
    ''' <param name="clsID">分类顶ID</param>
    ''' <param name="isBest">是否推荐</param>
    ''' <param name="isTejia">是否特价</param>
    ''' <param name="serKey">搜索关键词</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getPrductList(Optional clsID As String = "", Optional isBest As Boolean = False, Optional isTejia As Boolean = False, Optional serKey As String = "") As String
        clsID = dValue(clsID, "1")
        Dim subIds As String = getSubIDs(clsID, True, True)
        Dim cond As String = " prd_clsID in(" & subIds & ")"
        If isBest Then
            cond &= " and prd_isbest=1 "
        End If
        If isTejia Then
            cond &= " and prd_istejia=1 "
        End If
        If serKey <> "" Then
            cond &= " and (prd_Name like '%" & serKey & "%'  or prd_Intro like '%" & serKey & "%' or prd_Content like '%" & serKey & "%' ) "
        End If
        Dim field As String = "Id,prd_Name,prd_ModelNo,prd_clsID,prd_Picx,prd_Intro,prd_tParameter"
        Dim strNewlst As New StringBuilder
        Dim dtNew As Data.DataTable = GetDataTable("Products", field, cond, " prd_pSort1 desc,prd_isbest,prd_istejia,prd_UpdTime desc")
        If dtNew IsNot Nothing Then
            Dim rCount As Integer = dtNew.Rows.Count
            Dim nId, prd_Name, prd_ModelNo, prd_clsID, prd_Picx, prd_Intro, prd_tParameter As String
            Dim clsName, strImglog, href, strRecLine As String
            For r As Integer = 0 To rCount - 1
                nId = dtNew.Rows(r)("id")
                prd_Name = dValue(dtNew.Rows(r)("prd_Name"), "")
                prd_ModelNo = dValue(dtNew.Rows(r)("prd_ModelNo"), "")
                prd_Intro = dValue(dtNew.Rows(r)("prd_Intro"), "")
                prd_clsID = dtNew.Rows(r)("prd_clsID")
                prd_Picx = dValue(dtNew.Rows(r)("prd_Picx"), "")
                prd_tParameter = dValue(dtNew.Rows(r)("prd_tParameter"), "")
                prd_tParameter = prd_tParameter.Replace(vbCrLf, "<br/>")
                clsName = getFiledvalue("cls_name", "classes", "id=" & prd_clsID)
                clsName = "<span class=gray> [" & clsName & "] </span>"
                If prd_Picx <> "" Then
                    strImglog = "<img src=""" & prd_Picx & """ height=""120""/>"
                Else
                    strImglog = "<img src=""/uploadfiles/product/noImg.jpg"" />"
                End If
                strRecLine = "<li><span><a href=""productdetail.aspx?pdid={0}"" target=""_blank"">{1}</a></span>" & _
                    "<p><a href=""productdetail.aspx?pdid={0}"" target=""_blank"">{2}<br>{3}</a></p></li>" & vbCrLf
                strNewlst.AppendFormat(strRecLine, nId, strImglog, prd_Name, prd_tParameter)
            Next
        End If
        Return strNewlst.ToString
    End Function

    ''' <summary>
    ''' 获取相关产品
    ''' </summary>
    ''' <param name="clsID">分类顶ID</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getPrductLike(Optional clsID As String = "", Optional selfid As String = "") As String

        Dim cond As String = " prd_clsID =" & clsID & " and id<>" & selfid


        Dim field As String = "top(5) Id,prd_Name,prd_ModelNo,prd_clsID,prd_Picx"
        Dim strNewlst As New StringBuilder
        Dim dtNew As Data.DataTable = GetDataTable("Products", field, cond, " prd_pSort desc,prd_isbest,prd_istejia,prd_UpdTime desc")
        If dtNew IsNot Nothing Then
            Dim rCount As Integer = dtNew.Rows.Count
            Dim nId, prd_Name, prd_ModelNo, prd_clsID, prd_Picx As String
            Dim clsName, strImglog, strRecLine As String
            For r As Integer = 0 To rCount - 1
                nId = dtNew.Rows(r)("id")
                prd_Name = dValue(dtNew.Rows(r)("prd_Name"), "")
                prd_ModelNo = dValue(dtNew.Rows(r)("prd_ModelNo"), "")
                'prd_Intro = dValue(dtNew.Rows(r)("prd_Intro"), "")
                prd_clsID = dtNew.Rows(r)("prd_clsID")
                prd_Picx = dValue(dtNew.Rows(r)("prd_Picx"), "")
                clsName = getFiledvalue("cls_name", "classes", "id=" & prd_clsID)
                clsName = "<span class=gray> [" & clsName & "] </span>"
                If prd_Picx <> "" Then
                    strImglog = "<img src=""" & prd_Picx & """ height=""80""/>"
                Else
                    strImglog = "<img src=""/uploadfiles/product/noImg.jpg"" />"
                End If
                strRecLine = "<li><a href=""productdetail.aspx?pdid={0}"" target=""_blank""><div>{1}</div><p>{2}</p></a></li>" & vbCrLf
                strNewlst.AppendFormat(strRecLine, nId, strImglog, prd_ModelNo)
            Next
        End If
        Return strNewlst.ToString
    End Function


    Dim parentCls As String = ""
    ''' <summary>
    ''' 获取父ID列表
    ''' </summary>
    ''' <param name="clsID">要获取的类ID</param>
    ''' <param name="linkChar">连接字符</param>
    ''' <param name="showType">显示方式 0:ID 1:名称</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getParentClass(clsID As String, Optional linkChar As String = ",", Optional showType As Integer = 0, Optional isCurrent As Boolean = True) As String
        If isCurrent Then parentCls = ""
        Dim cls_id, cls_name, cls_parentid As String
        Dim SQLStr As String = "select id,cls_name,cls_parentid from classes where id=" & clsID
        Dim dtCls As Data.DataTable = GetDataTableA(SQLStr)
        If dtCls IsNot Nothing Then
            cls_id = dtCls.Rows(0)("id")
            cls_name = dtCls.Rows(0)("cls_name")
            cls_parentid = dtCls.Rows(0)("cls_parentid")
            If parentCls = "" Then
                If showType = 0 Then parentCls = cls_id Else parentCls = cls_name
            Else
                If showType = 0 Then parentCls = cls_id & linkChar & parentCls Else parentCls = cls_name & linkChar & parentCls
            End If
            If cls_parentid <> 0 Then
                If cls_parentid <> 1 Then parentCls = getParentClass(cls_parentid, linkChar, showType, False)
            End If
        End If
        Return parentCls

    End Function

#End Region


#End Region

#Region "IP，PV，UV统计"

    ''' <summary>
    ''' 加入访问日志
    ''' </summary>
    ''' <param name="fromIP">访客IP</param>
    ''' <param name="pageURL">访问页面地址</param>
    ''' <param name="fromURL">来源地址</param>
    ''' <param name="userInfo">访客名称</param>
    ''' <remarks></remarks>
    Sub AddLogs(fromIP As String, pageURL As String, fromURL As String, userInfo As String)
        Dim SQLLog As String = "Insert into logs(log_Page,log_From,log_IP,log_UseInfo) values(@log_Page,@log_From,@log_IP,@log_UseInfo)"
        Dim sParameter() As Data.SqlClient.SqlParameter = { _
                        New Data.SqlClient.SqlParameter("@log_Page", pageURL) _
                      , New Data.SqlClient.SqlParameter("@log_From", fromURL) _
                      , New Data.SqlClient.SqlParameter("@log_IP", fromIP) _
                      , New Data.SqlClient.SqlParameter("@log_UseInfo", userInfo) _
                 }
        ExecuteSQL(SQLLog, sParameter)
    End Sub


#End Region






End Module


